package stack;

import org.junit.Test;

import java.util.Stack;

/*
 *@description 简化路径
 *@author hkfan
 *@version 1.0
 *@create 2025/6/6 14:51
 */
public class LeetCode_71 {
    @Test
    public void test(){
        System.out.println(simplifyPath("/a/./b/../../c/"));
    }
    public String simplifyPath(String path) {
        //1. 定义
        Stack<String> stack = new Stack<>();
        StringBuilder res = new StringBuilder();
        //2. path加入stack
        for (String s : path.split("/")) {
            if (!s.equals("")){
                stack.add(s);
            }
        }
        //3. 循环处理stack
        while (!stack.isEmpty()){
            String temp = stack.pop();
            if ((!temp.equals("..")) && (!temp.equals("."))){
                res.insert(0,"/"+temp);
            }
            if (temp.equals("..")){
                int size = 1;
                while (!stack.isEmpty()&&size>0){
                    String pop = stack.pop();
                    if (pop.equals("..")){
                        size++;
                    } else if (pop.equals(".")) {
                        continue;
                    }else {
                        size--;
                    }
                }
            }
        }
        return (res.toString().equals(""))?"/":res.toString();

    }
}
